package com.niubi.yygh.user.controller;


import com.niubi.yygh.common.result.R;
import com.niubi.yygh.model.user.Patient;
import com.niubi.yygh.user.service.PatientService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import util.AuthContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author 牛俊杰
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {
  @Autowired
  PatientService patientService;

  //获取就诊人列表
  @GetMapping("auth/findAll")
  public R findAll(HttpServletRequest request) {
    Long userId = AuthContextHolder.getUserId(request);
    List<Patient> list = patientService.findAllUserId(userId);
    return R.ok().data("list",list);
  }

  //添加就诊人
  @PostMapping("auth/save")
  public R savePatient(@RequestBody Patient patient, HttpServletRequest request) {
    Long userId = AuthContextHolder.getUserId(request);
    patient.setUserId(userId);
    patientService.save(patient);
    return R.ok();
  }

  //根据id获取就诊人信息
  @GetMapping("auth/get/{id}")
  public R getPatient(@PathVariable Long id) {
    Patient patient = patientService.getPatientId(id);
    return R.ok().data("patient",patient);
  }
  //修改就诊人
  @PostMapping("auth/update")
  public R updatePatient(@RequestBody Patient patient) {
    patientService.updateById(patient);
    return R.ok();
  }
  //删除就诊人
  @DeleteMapping("auth/remove/{id}")
  public R removePatient(@PathVariable Long id) {
    patientService.removeById(id);
    return R.ok();
  }
  @ApiOperation(value = "获取就诊人")
  @GetMapping("inner/get/{id}")
  public Patient getPatientOrder(
          @ApiParam(name = "id", value = "就诊人id", required = true)
          @PathVariable("id") Long id) {
    return patientService.getById(id);
  }
}

